﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;

namespace 包覆门.PackageManager
{
    public partial class PackageList : Form
    {
        public PackageList()
        {
            InitializeComponent();
            this.dgPackage.AutoGenerateColumns = false;
        }
        private PackageDAL dal = new PackageDAL();
        //新增
        private void tsbAdd_Click(object sender, EventArgs e)
        {
            PackageEditing pe = new PackageEditing();
            pe.IsNew = true;
            if(pe.ShowDialog()==DialogResult.OK){
                LoadData();
            }
        }

        private void tsbClose_Click(object sender, EventArgs e)
        {
            Close();
        }
        //窗体加载
        private void PackageList_Load(object sender, EventArgs e)
        {
            LoadData();
        }
        //清空操作
        private void btnclear_Click(object sender, EventArgs e)
        {
            this.tbptype.Text = "";
        }
        //数据加载
        public void LoadData() {
            this.dgPackage.DataSource = dal.GetpackageListAll(this.tbptype.Text);
        }
        //编辑
        private void tsbEdit_Click(object sender, EventArgs e)
        {
            int count = dgPackage.SelectedRows.Count;
            if (count <= 0)
            {
                MessageBox.Show("请选中要编辑的行");
                return;
            }
            PackageEditing pe = new PackageEditing();
            pe.IsNew = false;
            pe.EditingId = dgPackage.SelectedRows[0].Cells["pid"].Value.ToString();
            if (pe.ShowDialog() == DialogResult.OK)
            {
                LoadData();
            }
        }
        //删除
        private void tsbDel_Click(object sender, EventArgs e)
        {
            int count = dgPackage.SelectedRows.Count;
            if (count <= 0)
            {
                MessageBox.Show("请选中要删除的行");
                return;
            }
            string pid = dgPackage.SelectedRows[0].Cells["pid"].Value.ToString();
            String info = "您确定要删除包装类别：" + dgPackage.SelectedRows[0].Cells["ptype"].Value.ToString() + "吗";
            if (DialogResult.OK == MessageBox.Show(info, "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information))
            {
                if (dal.DelById(pid))
                {
                    MessageBox.Show("删除成功");
                    LoadData();
                }
                else
                {
                    MessageBox.Show("删除失败");
                    LoadData();
                }
            }
        }
        //query查询
        private void btnQuery_Click(object sender, EventArgs e)
        {
            LoadData();
        }
        //关闭时操作
        private void PackageList_FormClosing(object sender, FormClosingEventArgs e)
        {
            mainform.pl = null;
        }
    }
}
